<template>
  <div class="line_2">
    <div class="sort_title"><i class="icon"></i>标签</div>
    <el-checkbox-group ref="tagTabs" v-model="checkboxGroup1" size="small" @change="tagChange">
      <el-checkbox v-for="item in tagTabs" :key="item.name" :label="item.name" border class="tag_item"></el-checkbox>
      <span
        v-if="!showAllBtn && boxHeight > 50"
        class="put_up"
        @click="
          $refs.tagTabs.$el.style.height = '36px'
          showAllBtn = true
        "
        >收起</span
      >
    </el-checkbox-group>
    <el-button
      v-if="showAllBtn"
      class="all_btn"
      icon="el-icon-arrow-down"
      @click="
        $refs.tagTabs.$el.style.height = 'auto'
        showAllBtn = false
      "
      >全部</el-button
    >
  </div>
</template>

<script>
import { requestMatrixLabelPage } from '@/api/chart/chart'
export default {
  data() {
    return {
      checkboxGroup1: ['全部'],
      showAllBtn: false,
      tagTabs: [],
      boxHeight: 0,
    }
  },
  async created() {
    let res = await requestMatrixLabelPage({ pageNo: 1, pageSize: 1000 })
    if (res.success) {
      this.tagTabs = [{ name: '全部' }, ...res.data.listObject]
    }
  },
  mounted() {
    let that = this
    window.onresize = () => {
      return (() => {
        that.$refs.tagTabs.$el.style.height = 'auto'
        that.count()
      })()
    }

    setTimeout(() => {
      that.count()
    }, 1000)
  },
  destroyed() {
    window.onresize = null
  },
  methods: {
    tagChange(val) {
      if (this.checkboxGroup1.length > 1 && this.checkboxGroup1.indexOf('全部') < this.checkboxGroup1.length - 1) {
        this.checkboxGroup1 = this.checkboxGroup1.filter((item) => item != '全部')
      } else {
        this.checkboxGroup1 = ['全部']
      }
      this.$emit('tag-change', this.checkboxGroup1)
    },
    count() {
      let w = this.$refs?.tagTabs?.$el?.offsetWidth || 0
      this.boxHeight = this.$refs?.tagTabs?.$el?.offsetHeight || 0
      if (this.boxHeight > 50) {
        this.showAllBtn = true
        this.$refs.tagTabs.$el.style.height = '36px'
      } else {
        this.showAllBtn = false
      }
      // this.tagTabs.length * 120
      // if (this.tagTabs.length * 120 > w) {
      //   this.showAllBtn = true
      // } else {
      //   this.showAllBtn = false
      // }
    },
  },
}
</script>

<style lang="scss" scoped>
.icon {
  width: 16px;
  height: 16px;
  background: url(../../assets/images/tag.png) no-repeat;
  display: inline-block;
  background-size: contain;
  vertical-align: middle;
  margin-right: 10px;
  margin-top: -3px;
}
.all_btn {
  width: 107px;
  height: 32px;
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  border: 1px solid rgba(0, 0, 0, 0.09);
  flex-grow: 0;
  flex-shrink: 0;
}
.sort_title {
  width: 102px;
  margin-right: 40px;
  height: 32px;
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  border: 1px solid rgba(0, 0, 0, 0.09);
  text-align: center;
  line-height: 30px;
  flex-shrink: 0;
  flex-grow: 0;
}
.line_2 {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  margin-bottom: 10px;
}
::v-deep .el-checkbox-group {
  // height: 36px;
  overflow: hidden;
}
::v-deep .el-checkbox.is-bordered.el-checkbox--small {
  min-width: 107px;
  height: 32px;
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  border: 1px solid rgba(0, 0, 0, 0.09);
  line-height: 32px;
  text-align: center;
  margin-right: 20px;
  margin-left: 0;
  margin-bottom: 5px;
  padding: 0;
  .el-checkbox__input {
    display: none;
  }
  .el-checkbox__label {
    padding: 0 6px;
    overflow: hidden;
    font-size: 14px;
    color: #7a838e;
  }
  &.is-checked {
    border: 1px solid var(--primary-color);
    color: var(--primary-color);
    margin: 0 20px 5px 0;
    .el-checkbox__label {
      color: var(--primary-color) !important;
      font-size: 14px;
    }
  }
}
.put_up {
  display: inline-block;
  width: 107px;
  height: 32px;
  line-height: 30px;
  text-align: center;
  margin-right: 20px;
  margin-left: 0;
  margin-bottom: 5px;
  font-size: 14px;
  color: var(--primary-color);
}
::v-deep .el-checkbox.is-bordered + .el-checkbox.is-bordered {
  margin-left: 0;
}
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__nav {
  border: none;
}
</style>
